package com.jh.user.repository;

import com.jh.user.pojo.BrandRate;
import com.jh.user.pojo.enums.PayType;
import com.jh.user.pojo.enums.UserGrade;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface BrandRateRepository extends JpaRepository<BrandRate,String>,JpaSpecificationExecutor<BrandRate>{

	@Query("select brandRate from BrandRate brandRate where brandRate.brandId=:brandId  and brandRate.channelId=:channelId " +
			"and brandRate.userGrade=:userGrade and brandRate.payType=:payType and brandRate.channelMerType=:channelMerType")
	BrandRate findBrandRate(@Param("brandId")long brandId, @Param("channelId")long channelId,
												 @Param("userGrade") UserGrade userGrade, @Param("payType")Short payType, @Param("channelMerType")String channelMerType);

	@Query("select brandRate from BrandRate brandRate where brandRate.brandId=:brandId  and brandRate.channelId=:channelId " +
			"and brandRate.userGrade=:userGrade and brandRate.payType=:payType ")
	BrandRate findBrandRate(@Param("brandId")long brandId, @Param("channelId")long channelId,
							@Param("userGrade") UserGrade userGrade, @Param("payType")Short payType);

	@Query("select brandRate from  BrandRate brandRate where brandRate.brandId=:brandid ")
	List<BrandRate>  findBrandRateBybrandid(@Param("brandid") long brandid);

	@Query("select brandRate from BrandRate brandRate where brandRate.channelId = :channelId and brandRate.userGrade = :grade and brandRate.payType = :payType")
	List<BrandRate> findBrandRate(@Param("channelId") long channelId, @Param("grade") String grade, @Param("payType") Short payType);

	@Query("select br from BrandRate br where br.brandId=:brandId and br.channelId in :channelIds")
	List<BrandRate> findBrandRate(@Param("brandId") long brandId,
								  @Param("channelIds")List<Long> channelIds);

    @Query("select brandRate from BrandRate brandRate where brandRate.brandId = :brandId and brandRate.channelId = :channelId and brandRate.payType = :payType and brandRate.channelMerType = :channelMerType ")
	List<BrandRate> findBrandRate(@Param("brandId") Long brandId, @Param("channelId") Long channelId, @Param("payType") Short payType, @Param("channelMerType") String channelMerType);

	@Query("select brandRate from BrandRate brandRate where brandRate.brandId=:brandId and brandRate.userGrade=:userGrade")
    List<BrandRate> findBrandRate(@Param("brandId")Long brandId, @Param("userGrade") UserGrade userGrade);

	@Query("select b from BrandRate b where b.channelId = :channelId and b.userGrade = :grade and b.brandId = :brandId and b.payType = '1' and b.status = '1'")
    BrandRate queryBrandRate(@Param("channelId") String channelId, @Param("grade") String grade, @Param("brandId") long brandId);

	@Query("select b from BrandRate b where b.brandId = :brandId")
	Page<BrandRate> queryBrandRateOrderByMinRate(@Param("brandId") long brandId, Pageable pageable);

	@Query("select b from BrandRate b where b.brandId = :brandId and b.channelId = :channelId and b.userGrade = :userGrade and b.payType = :payType")
	BrandRate queryBrandRateByFour(@Param("brandId") Long brandId, @Param("channelId") Long channelId, @Param("userGrade") UserGrade userGrade, @Param("payType") Short payType);

	@Query("select b from BrandRate b where b.channelId = :channelId and b.userGrade = :grade and b.brandId = :brandId and b.status = '1'")
	List<BrandRate> getBrandRateByChannelIdAndGrand(@Param("channelId") long channelId, @Param("grade") UserGrade grade, @Param("brandId") long brandId);

	@Query("select b from BrandRate b where b.userGrade = :grade")
    List<BrandRate> queryByUserGrade(@Param("grade") UserGrade grade);
}
